package com.meevle.utilities;

import org.red5.core.ApplicationSettings;
import org.red5.logging.Red5LoggerFactory;
import org.slf4j.Logger;

import com.meevle.business.UserDelegate;
import com.meevle.dao.SetDAO;
import com.meevle.dao.SlotCommentDAO;
import com.meevle.dao.SlotDAO;
import com.meevle.dao.UserDAO;
import com.meevle.vo.SetVO;
import com.meevle.vo.SlotCommentVO;
import com.meevle.vo.SlotVO;
import com.meevle.vo.UserVO;

public class DaoToVoAdapter {

	private static Logger log = Red5LoggerFactory.getLogger(DaoToVoAdapter.class, "Meevle");
	private volatile static DaoToVoAdapter instance = null;
	
	private DaoToVoAdapter()
	{}
	
	public static DaoToVoAdapter getInstance () {
        if (instance == null)
        {
        	synchronized (DaoToVoAdapter.class)
        	{
        		if (instance == null) instance = new DaoToVoAdapter();
			}
        }
        return instance;
    }
	
	/**
	 * Get a com.meevle.vo.ProjectVO object from a com.meevle.doa.ProjectDOA object
	 * @param projectdao
	 * @return
	 * @throws NullPointerException if some parameter within projectdao of projectdao itself was null
	 */
	public SetVO getSetFromSetDAO(SetDAO projectdao) throws NullPointerException
	{
		SetVO pr = null;
		try {
			pr = new SetVO();
			pr.setName(projectdao.getName());
			pr.setOwner( getUserFromUserDAO(projectdao.getOwner() ) );
		} catch (NullPointerException e) {
			log.warn("projectdao == null => returned project is null as well\n" + e.getMessage());
			throw e;
		}
		return pr;
	}
	
	/**
	 * Transforms a com.meevle.dao UserDAO object into a com.meevle.vo UserVO object
	 * @param userdao the com.meevle.dao UserDAO object that needs to be transformed
	 * @return a com.meevle.vo UserVO object
	 * @throws NullPointerException if the parameter is null
	 */
	public UserVO getUserFromUserDAO(UserDAO userdao)throws NullPointerException
	{
		UserVO u = null;
		try {
			u = new UserVO();
			u.setUsername(userdao.getUsername());
		} catch (NullPointerException e) {
			log.warn("User is null");
			throw e;
		}
		return u;
	}
	
	/**
	 * Transforms an com.meevle.dao.TrackDAO object into an com.meevle.vo.TrackVO object
	 * @param trackDao the com.meevle.dao.TrackDAO object
	 * @return an com.meevle.vo.TrackVO object
	 * @throws NullPointerException if the parameter is null or a variable of the parameter was null
	 */
	public SlotVO getSlotFromSlotDAO(SlotDAO trackDao) throws NullPointerException
	{
		SlotVO tr = null;
		
		try {
			tr = new SlotVO();
			tr.setDirectory( trackDao.getDirectory() );
			tr.setFilename( trackDao.getFilename() );
			tr.setName( trackDao.getName() );
			tr.setOwner( getUserFromUserDAO( trackDao.getOwner() ) );
			tr.setTimeCreated(trackDao.getTimeCreated());
			tr.setIndex(trackDao.getSlotIndex());
		} catch (NullPointerException e) {
			log.warn("trackdao is null => returned track is null\n" + e.getMessage());
			throw e;
		}
		return tr;
	}

	public SlotCommentVO getSlotCommentVOFromSlotCommentDAO(SlotCommentDAO commentDao) {
		SlotCommentVO commentVo = new SlotCommentVO();
		
		try {
			commentVo.setComment(commentDao.getComment());
			commentVo.setRating(commentDao.getRating());
			commentVo.setSlot( getSlotFromSlotDAO(commentDao.getSlot()) );
			commentVo.setAuthor(getUserFromUserDAO(commentDao.getAuthor()));
			commentVo.setTimeStamp(commentDao.getTimestamp());
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return commentVo;
	}
}
